package com.rainbow.gray.framework.context;

import org.springframework.beans.factory.annotation.Autowired;

import com.rainbow.gray.framework.adapter.PluginAdapter;
import com.rainbow.gray.framework.constant.GrayConstant;

public abstract class AbstractStrategyContextHolder implements PluginContextHolder, StrategyContextHolder {
    @Autowired
    protected PluginAdapter pluginAdapter;

    @Override
    public String getContext(String name) {
        return getHeader(name);
    }

    @Override
    public String getContextRouteVersion() {
        String versionValue = getContext(GrayConstant.N_D_VERSION);
//        if (StringUtils.isEmpty(versionValue)) {
//            versionValue = getRouteVersion();
//        }

        return versionValue;
    }

    @Override
    public String getContextRouteRegion() {
        String regionValue = getContext(GrayConstant.N_D_REGION);
//        if (StringUtils.isEmpty(regionValue)) {
//            regionValue = getRouteRegion();
//        }

        return regionValue;
    }

    @Override
    public String getContextRouteEnvironment() {
        String environmentValue = getContext(GrayConstant.N_D_ENVIRONMENT);
//        if (StringUtils.isEmpty(environmentValue)) {
//            environmentValue = getRouteEnvironment();
//        }

        return environmentValue;
    }

    @Override
    public String getContextRouteAddress() {
        String addressValue = getContext(GrayConstant.N_D_ADDRESS);
//        if (StringUtils.isEmpty(addressValue)) {
//            addressValue = getRouteAddress();
//        }

        return addressValue;
    }

    @Override
    public String getContextRouteVersionWeight() {
        String versionWeightValue = getContext(GrayConstant.N_D_VERSION_WEIGHT);
//        if (StringUtils.isEmpty(versionWeightValue)) {
//            versionWeightValue = getRouteVersionWeight();
//        }

        return versionWeightValue;
    }

    @Override
    public String getContextRouteRegionWeight() {
        String regionWeightValue = getContext(GrayConstant.N_D_REGION_WEIGHT);
//        if (StringUtils.isEmpty(regionWeightValue)) {
//            regionWeightValue = getRouteRegionWeight();
//        }

        return regionWeightValue;
    }

    @Override
    public String getContextRouteIdBlacklist() {
        String idBlacklistValue = getContext(GrayConstant.N_D_ID_BLACKLIST);
//        if (StringUtils.isEmpty(idBlacklistValue)) {
//            idBlacklistValue = getRouteIdBlacklist();
//        }

        return idBlacklistValue;
    }

    @Override
    public String getContextRouteAddressBlacklist() {
        String addressBlacklistValue = getContext(GrayConstant.N_D_ADDRESS_BLACKLIST);
//        if (StringUtils.isEmpty(addressBlacklistValue)) {
//            addressBlacklistValue = getRouteAddressBlacklist();
//        }

        return addressBlacklistValue;
    }

    public PluginAdapter getPluginAdapter() {
        return pluginAdapter;
    }

    // 如果配置了内置条件Header，强制使用内置条件Header的模式
    // 该模式只适用于服务层。不希望在服务层处理的这么复杂，且一般情况下，不会在服务层内置Header
    // 该模式不适用于网关层。网关层会在过滤器中预先塞入内置Header
    protected boolean isInnerConditionHeaderForced() {
        return false;
    }
}